/**
 * Copyright (C) 2010 DustedPixels.com
 */
package com.dustedpixels.cirt.model.machine.spectrum.ula;

import com.dustedpixels.cirt.model.core.Circuit;
import com.dustedpixels.cirt.model.core.In;
import com.dustedpixels.cirt.model.core.Out;
import com.dustedpixels.cirt.model.core.Pins;
import com.dustedpixels.cirt.model.core.Script;

/**
 * Decodes given address counter into screen and attribute memory address.
 *
 * @author Michal Pociecha-Los (micapolos@gmail.com)
 */
@Circuit
public final class AddressDecoder {
  @In @Pins(13) public int addressIn;
  @In public boolean attr;
  @Out @Pins(13) public int addressOut;
  
  @Script
  public void decode() {
    int in0 = addressIn & 0x001F;

    if (!attr) {
      addressOut = in0 | ((addressIn >>> 3) & 0x00E0) | ((addressIn << 3) & 0x0700) | (addressIn & 0x1800); 
    } else {
      addressOut = in0 | ((addressIn >>> 3) & 0x03E0) | 0x1800;
    }
  }
}
